<?php
class seo{
	private $http_interface='auto';	//'auto': autodetect, 'curl' or 'fopen'
    public function GoogleRank($domain){
		$url='http://toolbarqueries.google.com/tbr';
		$get_variables=array('client'=>'navclient-auto','ch'=>$this->CheckHash($this->hashURL($domain)),'features'=>'Rank','q'=>'info:'.$domain,'num'=>100,'filter'=>0);
    	$response=$this->httpRequest($url,$get_variables);
    	if($response!==false){
    		if(substr($response['code'],0,1)=='2'){
    			preg_match('/Rank_([0-9]+):([0-9]+):([0-9]+)/si',$response['body'],$matches);
    			return ($matches[3])?$matches[3]:0;
    		}
		}
		return 0;
    }
	public function AlexaRank($domain){
		$url = 'http://data.alexa.com/data';
		$get_variables=array('cli'=>10,'dat'=>'snbamz','url'=>urlencode($domain));
    	$response=$this->httpRequest($url,$get_variables);
    	if($response!==false){
    		if(substr($response['code'],0,1)=='2'){
    			preg_match('/\<popularity url\="(.*?)" TEXT\="([0-9]+)"/si',$response['body'],$matches);
				return ($matches[2])?$matches[2]:4294967295;
    		}
    	}
		return 4294967295;
	}
	public function httpRequest($url,$get=null,$post=null,$headers=null){
    	$interface = $this->http_interface;
	    if($this->http_interface =='auto'){
		    if(function_exists('curl_exec')){
		      $interface = 'curl';
		    }else{
		      $interface = 'fopen';
		    }
	    }
	    if($interface == 'curl'){
	      return $this->curlRequest($url,$get,$post,$headers);
	    }elseif($interface == 'fopen'){
	      return $this->fopenRequest($url,$get,$post,$headers);
	    }else{
	      return false;
	    }
	}
	private function curlRequest($url,$get=null,$post=null,$headers=null){
	    $ch = curl_init();
	    if(is_array($get)){
	      $get='?'.str_replace('&amp;','&',urldecode(http_build_query($get)));
	    }else{
	      $get=null;
	    }
	    curl_setopt($ch, CURLOPT_URL,$url.$get);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	    if(is_array($post)){
	      curl_setopt($ch, CURLOPT_POST, true);
	      curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
	    }
	    if(is_array($headers)){
	      curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
	    }
	    $response=curl_exec($ch);
	    $code=curl_getinfo($ch,CURLINFO_HTTP_CODE);
	    curl_close($ch);
	    return array('body'=>$response,'code'=>$code);
	}
	private function fopenRequest($url,$get=null,$post=null,$headers=null){
	    $http_options=array('method'=>'GET','timeout'=>3);
	    if(is_array($headers)){
	      $headers=implode("\r\n",$headers) . "\r\n";
	    }else{
	      $headers='';
	    }
	    if(is_array($get)){
	      $get='?'.str_replace('&amp;','&',urldecode(http_build_query($get)));
	    }else{
	      $get=null;
	    }
	    if(is_array($post)){
	      $post=str_replace('&amp;','&',urldecode(http_build_query($post)));
	      $http_options['method'] = 'POST';
	      $headers="Content-type: application/x-www-form-urlencoded\r\n"."Content-Length:".strlen($post)."\r\n".$headers;
	      $http_options['header']=$headers;
	      $http_options['content']=$post;
	    }else{
	      $post='';
	      $http_options['header']=$headers;
	    }
	    $context=stream_context_create(array('http'=>$http_options));
	    $response=@file_get_contents($url.$get,null,$context);
	    return array('body'=>$response!==false?$response:'Request failed','code'=>$response!==false?'200':'400');
	}
	function strToNum($Str, $Check,$Magic){
		$Int32Unit = 4294967296;
		$length=strlen($Str);
		for($i=0;$i<$length;$i++){
			$Check *= $Magic;
			if($Check >= $Int32Unit){
				$Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
				$Check = ($Check < -2147483648)? ($Check + $Int32Unit) : $Check;
			}
			$Check += ord($Str{$i});
		}
		return $Check;
	}
	function hashURL($String){
		$Check1 = $this->strToNum($String, 0x1505, 0x21);
		$Check2 = $this->strToNum($String, 0, 0x1003F);
		$Check1 >>= 2;
		$Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
		$Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
		$Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);
		$T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
		$T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
		return ($T1 | $T2);
	}
	function checkHash($Hashnum){
		$CheckByte = 0;
		$Flag = 0;
		$HashStr = sprintf('%u', $Hashnum) ;
		$length = strlen($HashStr);
		for($i=$length-1;$i>= 0;$i --){
			$Re = $HashStr{$i};
			if (1 === ($Flag % 2)) {
				$Re += $Re;
				$Re = (int)($Re / 10) + ($Re % 10);
			}
			$CheckByte += $Re;
			$Flag ++;
		}
		$CheckByte%=10;
		if(0!== $CheckByte){
			$CheckByte=10-$CheckByte;
			if(1 ===($Flag % 2)){
				if (1 === ($CheckByte % 2)) {$CheckByte += 9;}
				$CheckByte >>= 1;
			}
		}
		return '7'.$CheckByte.$HashStr;
	}
}
?>